package cn.com.yiqi.wsc.service.city.impl;

import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultStatusEnum;
import cn.com.yiqi.wsc.dao.city.CityDao;
import cn.com.yiqi.wsc.dao.city.criteria.CityCriteria;
import cn.com.yiqi.wsc.entity.city.City;
import cn.com.yiqi.wsc.service.city.PortalCityService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 何刚
 * @ClassName: ${class}
 * @Description: TODO
 * @date 2017/3/6
 */
@Service
public class PortalCityServiceImpl implements PortalCityService {

    @Autowired
    private CityDao cityDao;

    @Override
    public Result getCodeAndCityNameByCityCode(CityCriteria criteria) {
        Result result = new Result();

        result.setStatus(ResultStatusEnum.操作成功.getCode());

        String cityCode = criteria.getCityCode();
        if (StringUtils.isEmpty(cityCode)) {
            //顶级的code为0
            criteria.setCityCode("0");
        }


        List<City> cityList = cityDao.getCodeAndCityNameByCityCode(criteria);

        if (cityCode.length() == 4) {
            //如果是最后一级 不反返回children
            for (City city : cityList) {
                city.setChildren(null);
            }
        }


        result.putData("list", cityList);
        return result;
    }
}
